/**
 * ImageListAdapt.java Create on 2012-8-1     
 *      
 * Copyright (c) 2012-8-1 by yiming     
 *      
 * @author <a href="alenzhangym@hotmail.com">Yiming</a>     
 * @version 1.0 
 */

package com.yimi.content;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable; 
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView; 

import com.yimi.image.FImageActivity;
import com.yimi.image.ImageActivity;
import com.yimi.image.R;
import com.yimi.search.AHApplication;
import com.yimi.service.BaseImageItem;
import com.yimi.util.ImageSearchUtil;

import java.lang.ref.SoftReference;
import java.util.HashMap;
import java.util.Map;

/**
 * TODO
 * 
 * @author Yiming
 * @version 1.0
 */
public class ImageListAdapt extends BaseAdapter {
    protected LayoutInflater inflater;
    private ImageListModel model;
   
    private Activity currentActivity;
    private AHApplication app;
    public static Bitmap srcBitMap;
    private Map<String,ViewHolder> cacheView;
    public ImageListAdapt(Activity searchActivity, ImageListModel model) {
        currentActivity = searchActivity;
        cacheView = new HashMap<String, ViewHolder>();
        app = (AHApplication) currentActivity.getApplication();
        this.inflater = searchActivity.getLayoutInflater();
        this.model = model;
        BitmapDrawable b = (BitmapDrawable) app.getResources()
                .getDrawable(R.drawable.kb_loading);
        srcBitMap = b.getBitmap();
    }

    /*
     * (non-Javadoc)
     * @see android.widget.Adapter#getCount()
     */
    @Override
    public int getCount() {
        return model.getSize();
    }

    /*
     * (non-Javadoc)
     * @see android.widget.Adapter#getItem(int)
     */
    @Override
    public Object getItem(int arg0) {
        return model.getImageItem(arg0);
    }

    /*
     * (non-Javadoc)
     * @see android.widget.Adapter#getItemId(int)
     */
    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    /*
     * (non-Javadoc)
     * @see android.widget.Adapter#getView(int, android.view.View,
     * android.view.ViewGroup)
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Log.e("Adapter", "start to getview");
        BaseImageItem baseItem = model.getImageItem(position);
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.taskitem, null);
            holder.imageView = (ImageView) convertView
                    .findViewById(R.id.taskicon);
            this.cacheView.put(baseItem.getPreviewURL(), holder);
            holder.imageView.setImageBitmap(srcBitMap);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        String path =  baseItem.getPreviewURL();
        int index = path.lastIndexOf('/');
        path = path.substring(index + 1);
        byte[]  bs = null ;
        if(baseItem.getLocation()==BaseImageItem.INSDCARD){
            Log.e("Adapter", "baseItem.getLocation()==BaseImageItem.INSDCARD"); 
            bs = ImageSearchUtil.getPictureFromFile(ImageSearchUtil.previewFilePath ,path);
        }else if(baseItem.getLocation()==BaseImageItem.INROM){
            bs = ImageSearchUtil.getPictureFromFile(ImageSearchUtil.romPreviewFilePath ,path);
        }else if(baseItem.getLocation()==BaseImageItem.INMEM){
         ;
        }
        if(bs!=null)
        {
            Bitmap bitmap = BitmapFactory.decodeByteArray(bs, 0, bs.length);
            holder.imageView.setImageBitmap(bitmap); 
        }
        holder.imageView.setOnClickListener(new OnQueryListener(baseItem)); 
        Log.e("baseItem", "baseItem is" + baseItem.getFileURL()); 
        Log.e("Adapter", "end  getview");
        return convertView;
    }

    public final class ViewHolder {
        public ImageView imageView; 
    }

    /**
     * @param previewItem
     */
    public void updateItem(final BaseImageItem previewItem) { 
        Log.e("updateItem", "previewItem location is "+previewItem.getLocation());
        this.model.updateItem(previewItem);
    }

    private class OnQueryListener implements OnClickListener {
        private BaseImageItem baseItem;

        OnQueryListener(BaseImageItem baseItem) {
            this.baseItem = baseItem;
        }

        /*
         * (non-Javadoc)
         * @see android.view.View.OnClickListener#onClick(android.view.View)
         */
        @Override
        public void onClick(View v) {
                Intent intent = new Intent(currentActivity,
                        FImageActivity.class);
                Bundle loginBundle = new Bundle();
                loginBundle.putParcelable("baseImageItem", baseItem);
                intent.putExtras(loginBundle);
                currentActivity.startActivity(intent);
             
        }

    }

  
}
